// Copyright (c) 2010. Highly Intelligent Solutions.
// All rights reserved.
var HiS;
if(!HiS)
{
	HiS = {};
}
if(!HiS.Ui)
{
	HiS.Ui = {};
}
/*Constructor*/
HiS.Ui.SelectPopup= function(target, opts)
{
	this.init(target, opts);
};
/*Propiedades*/
HiS.Ui.SelectPopup.prototype.selectResult=null;
HiS.Ui.SelectPopup.prototype.dataValue=null;
HiS.Ui.SelectPopup.prototype.dataText=null;
HiS.Ui.SelectPopup.prototype.selectedItem=null;
HiS.Ui.SelectPopup.prototype.selectedIdex=0;
HiS.Ui.SelectPopup.prototype.ajaxRequest=null;
HiS.Ui.SelectPopup.prototype.target=null;
HiS.Ui.SelectPopup.prototype.paramRequest=null;
HiS.Ui.SelectPopup.prototype.active=null;
/*Metodos*/
HiS.Ui.SelectPopup.prototype.init = function(target, opts)
{
	
	this.selectResult = document.createElement('select');
	this.selectResult.size = 10;
	var contect = document.createElement('div'); 
	this.selectResult.style.display = "none"; 
	contect.style.position = "absolute";	
	contect.appendChild(this.selectResult);
	if(target)
	{		
		var groupTarget=document.getElementById(target);
		groupTarget.parentNode.appendChild(contect);			
		this.selectResult.id =target + "_HiStargetResponse";				
		this.target=target;
		
		var HisSelectPopup=this;
		var targetInitHis = document.getElementById(target);						
		this.addEventListener(targetInitHis, 'keypress', function(event)
		{ 
			var txt =(event.srcElement)?event.srcElement:this;
			var tecla=event.keyCode?event.keyCode:event.which;
			if(!(tecla== 27 || tecla== 13 ||tecla== 9 ||tecla== 38 || tecla== 40 || tecla== 37 || tecla== 39 || tecla== 17 || tecla== 18 ))
			{
				HisSelectPopup.show();
				HisSelectPopup.search();
			}
				  
		}, false);
		this.addEventListener(targetInitHis, 'keyup', function(event)
				{ 
					var txt =(event.srcElement)?event.srcElement:this;
					var tecla=event.keyCode?event.keyCode:event.which;
					if(!(tecla== 38 || tecla== 40 || tecla== 37 || tecla== 39 || tecla== 17 || tecla== 18 ))
					{
						HisSelectPopup.search();
					}
						  
				}, false);
		this.addEventListener(targetInitHis, 'keydown',function( event ) {
			//alert(event.keyCode?event.keyCode:event.which);
			if ( HisSelectPopup.active ) {
			switch(event.keyCode?event.keyCode:event.which) {				
			case 38://UP
				HisSelectPopup.activeItem(-1);						
				break;
			case 40://Down
				HisSelectPopup.activeItem(+1);						
				break;			
			case 27:	
			case 13://Enter	 
					HisSelectPopup.hide();
					var items=HisSelectPopup.selectResult.options;
					var txt = document.getElementById(HisSelectPopup.target);
					txt.value=items[HisSelectPopup.selectedIdex].text;	
				break;
			case 9:	HisSelectPopup.hide();
					break;
			case 37:
			case 39:
			case 17:
			case 18://Ignore
				//break;
			default:
					
				break;
			}
			}	
		}, false);
	
		this.addEventListener(targetInitHis, 'blur', function(e)
		{ 
			if(!HisSelectPopup.active)
			 setTimeout(function() {
				 HisSelectPopup.hide();
				}, 150 );
				
		}, false);
		this.addEventListener(this.selectResult, 'blur', function(e)
				{ 
					 setTimeout(function() {
						 HisSelectPopup.hide();
						}, 150 );
						
				}, false);
		if(opts)
		{			
			if(opts.dataText)
			{
					this.dataValue=opts.dataValue;
					this.dataText=opts.dataText ;
			}
			if(opts.width)
			{
				this.selectResult.style.width = opts.width  + 'px';
			}
			if(opts.height)
			{
				this.selectResult.style.height = opts.height  + 'px';
			}
			if(opts.ajaxRequest)
			{
				this.ajaxRequest=opts.ajaxRequest;
				SubmitAjax(this.ajaxRequest,this,'XML');				
			}
		}		
		
	}	
	
};

HiS.Ui.SelectPopup.prototype.addEventListener = function(element, eventType, handler, capture)
{
	try
	{
		if (element.addEventListener)
		{
			element.addEventListener(eventType, handler, capture);
		}
		else if (element.attachEvent)
		{
			element.attachEvent('on' + eventType, handler);
		}
	}
	catch (e) {}
};

HiS.Ui.SelectPopup.prototype.show = function()
{		
	this.active=true;
	this.selectResult.style.display = ""; 
};

HiS.Ui.SelectPopup.prototype.hide = function()
{
	this.active=false;
	this.selectResult.style.display = "none";
		

};

HiS.Ui.SelectPopup.prototype.ajaxResponse = function(res)
{
	
	var xmlObj=res.documentElement;	
	this.active=false;
	if(xmlObj.childNodes.length==0)
		this.selectResult.options=null;		
	
	for(i=0;i< xmlObj.childNodes.length;i++)
	{
		var obj = xmlObj.childNodes[i];
		var div = document.createElement('option');  	  	
		if(i==this.selectedIdex)
			 div.selected=true;
		else			
			 div.selected=false;
		
		if(obj.attributes.length)
		{	
			 for (var a=0; a<obj.attributes.length-1; a++)
			 {
				 var atributeName=(obj.attributes[a].nodeName).toString();
				 var atributeValue=(obj.attributes[a].nodeValue||"").toString();
				 div.value+= ("'"+atributeName + "' : '"+ atributeValue+"'");
				 if(a< obj.attributes.length-1)
					 div.value+=",";
			 }		
			 div.value="selectedItem={" + div.value + "};";		
			 div.text=obj.getAttribute(this.dataText);
		}
		else
		{	
			div.value="selectedItem={ text:'" + obj.nodeValue + "' };";
			div.text=obj.nodeValue;
		}
				
		 try {
			 	this.selectResult.add(div, null); // standards compliant; doesn't work in IE
			  }
			  catch(ex) {
				this.selectResult.add(div); // IE only
			  }		
	}	
};
HiS.Ui.SelectPopup.prototype.activeItem= function(index)
{
	this.selectedIdex =this.selectedIdex+index;	
	var items=this.selectResult.options;
	if(this.selectedIdex>=items.length-1)
		this.selectedIdex =items.length-1;
	else
		if(this.selectedIdex<0)
			this.selectedIdex=0;		
	
	items[this.selectedIdex].selected = true;	
	this.selectedItem=eval(items[this.selectedIdex].value);
	document.getElementById(this.target).value=items[this.selectedIdex].text;
	if(this.selectedItemChange!=null)
		this.selectedItemChange();
		
};
HiS.Ui.SelectPopup.prototype.search= function()
{
	var items=this.selectResult.options;
	var txt = document.getElementById(this.target);
	for(i=0;i<items.length;i++)
	{
		//if( items[i].text.toUpperCase().startsWith(txt.value.toUpperCase()) )
		if(items[i].text.toUpperCase().indexOf(txt.value.toUpperCase())!=-1)
		{
			items[i].selected = true;
			this.selectedIdex = i;
			this.selectedItem=eval(items[this.selectedIdex].value);
			if(this.selectedItemChange!=null)
				this.selectedItemChange();
			break;
		}
	}
		
};
HiS.Ui.SelectPopup.prototype.selectedItemChange =null;

